package com.software.cool.api.admin.v1.controller;

import com.software.cool.api.admin.v1.service.impl.AmV1SysUserService;
import com.software.cool.api.admin.v1.vo.AdminV1Result;
import com.software.cool.utils.OssClientUtils;
import com.software.cool.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

@Api(value = "管理端-公共接口", tags = "管理端-公共接口")
@RestController
@Slf4j
@RequestMapping("/admin/v1/common")
@CrossOrigin
public class CommonController extends BaseController {

    @Resource
    private AmV1SysUserService amV1SysUserService;

    /**
     * 上传uploadVideo
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadVideo", consumes = "multipart/form-data", headers = "content-type=multipart/form-data")
    public AdminV1Result uploadVideo(MultipartFile file, HttpServletRequest request)throws Exception{
        Map<String,Object> paramsMap=this.getParameterMap(request);
//        String token= CommonUtils.parseStringData(paramsMap.get("token"));
//        AdminV1Result result=getAdmin(token);
//        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success(amV1SysUserService.uploadVideo(file,paramsMap,request));
    }

    /**
     * 上传uploadVideo
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadImage", consumes = "multipart/form-data", headers = "content-type=multipart/form-data")
    public AdminV1Result uploadImage(MultipartFile file, HttpServletRequest request)throws Exception{
        Map<String,Object> paramsMap=this.getParameterMap(request);
//        String token= CommonUtils.parseStringData(paramsMap.get("token"));
//        AdminV1Result result=getAdmin(token);
//        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success(amV1SysUserService.uploadImage(file,paramsMap,request));
    }

    /**
     * 上传apk
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadApk", consumes = "multipart/form-data", headers = "content-type=multipart/form-data")
    public AdminV1Result uploadApk(MultipartFile file, HttpServletRequest request)throws Exception{
        Map<String,Object> paramsMap=this.getParameterMap(request);
//        String token= CommonUtils.parseStringData(paramsMap.get("token"));
//        AdminV1Result result=getAdmin(token);
//        if(result.getCode()==-1){ return result; }
        return AdminV1Result.success(amV1SysUserService.uploadApk(file,paramsMap,request));
    }

    /**
     * 显示图片
     * @param request
     * @param response
     */
    @RequestMapping("/showImage")
    public void showImage(String ossFileName,HttpServletRequest request, HttpServletResponse response){
        if(CommonUtils.isNotBlank(ossFileName)) {
            OssClientUtils.showImage(ossFileName, response, request);
        }
    }


    /**
     * 显示不在项目目录下的图片
     *
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/showImageByServer")
    public void showImageByServer(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.showImageByServer(filePath,request,response);
    }
    /**
     * 下载文件
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/downloadApk")
    public void downloadApk(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.downloadApk(filePath,request,response);
    }

    /**
     * 下载文件
     *
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/downloadExcel")
    public void downloadExcel(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.downloadExcel(filePath,request,response);
    }

    /**
     * 下载File文件
     *
     * @param filePath
     * @param request
     * @param response
     */
    @RequestMapping("/downloadFile")
    public void downloadFile(String filePath,HttpServletRequest request, HttpServletResponse response){
        amV1SysUserService.downloadFile(filePath,request,response);
    }




    @GetMapping("/showVideo")
    public void showVideo(HttpServletRequest request, HttpServletResponse response, String ossFileName)throws Exception{
        if(CommonUtils.isNotBlank(ossFileName)){
            String path = request.getContextPath();
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.write("<style>body{background-color: rgb(0, 0, 0);}</style><video controls autoplay muted name=\"media\"style=\"    position: absolute;\n" +
                    "    top: 0px;\n" +
                    "    right: 0px;\n" +
                    "    bottom: 0px;\n" +
                    "    left: 0px;\n" +
                    "    max-height: 100%;\n" +
                    "    max-width: 100%;\n" +
                    "    margin: auto;\" ><source src=\""+basePath+"/admin/v1/common/downloadVideo?ossFileName="+ossFileName+"\" type=\"video/mp4\"></video>");
            out.close();
        }

    }




    @ApiOperation(value = "下载", notes = "下载")
    @GetMapping("/downloadVideo")
    public void downloadVideo(String ossFileName,HttpServletResponse response)throws Exception{
        OssClientUtils.downloadStreamFile(ossFileName,response);
    }


}
